﻿using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TpBaseObjects.Base {

    public class TpAccountingTargetRow {

        #region Members

        private Range myRow;
        private Microsoft.Office.Interop.Excel.Application exApp;

        #endregion

        #region Constructor

        public TpAccountingTargetRow(Microsoft.Office.Interop.Excel.Application exApp, Range row) {
            this.myRow = row;
            this.exApp = exApp;
        }

        #endregion

        #region Properties

        public int AccountNo {
            get {
                int ret = 0;
                Range r1 = myRow.Cells[1, "D"];
                if (r1.Value != null) {
                    if (Int32.TryParse(r1.Value.ToString(), out ret)) {
                        return ret;
                    }
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, "D"];
                r1.NumberFormat = "0## ##";
                if (value > 99999) {
                    r1.NumberFormat = "### ###";
                }
                r1.HorizontalAlignment = Constants.xlLeft;
                r1.Value = value;
            }
        }

        public double Credit {
            get {
                double ret = 0.0;
                Range r1 = myRow.Cells[1, "F"];
                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out ret)) {
                        return ret;
                    }
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, "F"];
                r1.NumberFormat = "#,##0.00";
                if (value == 0) {
                    r1.Value = null;
                } else {
                    r1.Value = value;
                }
            }
        }

        public double Debit {
            get {
                double ret = 0.0;
                Range r1 = myRow.Cells[1, "E"];
                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out ret)) {
                        return ret;
                    }
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, "E"];
                r1.NumberFormat = "#,##0.00";
                if (value == 0) {
                    r1.Value = null;
                } else {
                    r1.Value = value;
                }
            }
        }

        public bool IsEmpty {
            get {
                bool ret = false;
                Range r1 = myRow.Cells[1, "A"];
                if (r1.Value != null) {
                    ret = true;
                }
                return ret;
            }
        }

        public double Tax {
            get {
                double ret = 0.0;
                Range r1 = myRow.Cells[1, "G"];
                if (r1.Value != null) {
                    if (Double.TryParse(r1.Value.ToString(), out ret)) {
                        return ret;
                    }
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, "G"];
                r1.NumberFormat = "#,##0.00";
                r1.Value = value;
            }
        }

        public int TaxKey {
            get {
                int ret = 0;
                Range r1 = myRow.Cells[1, "H"];
                if (r1.Value != null) {
                    if (Int32.TryParse(r1.Value.ToString(), out ret)) {
                        return ret;
                    }
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, "H"];
                r1.Value = value;
            }
        }

        public string Text {
            get {
                string ret = "";
                Range r1 = myRow.Cells[1, "C"];
                if (r1.Value != null) {
                    ret = Convert.ToString(r1.Value);
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, "C"];
                r1.Value = value;
            }
        }

        public DateTime VoucherDate {
            get {
                DateTime ret = new DateTime(1900, 1, 1);
                Range r1 = myRow.Cells[1, "A"];
                if (r1.Value != null) {
                    if (DateTime.TryParse(r1.Value.ToString(), out ret)) {
                        return ret;
                    }
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, "A"];
                r1.Value = value;
            }
        }

        public int VoucherNo {
            get {
                int ret = 0;
                Range r1 = myRow.Cells[1, "B"];
                if (r1.Value != null) {
                    if (Int32.TryParse(r1.Value.ToString(), out ret)) {
                        return ret;
                    }
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, "B"];
                r1.HorizontalAlignment = Constants.xlLeft;
                r1.Value = value;
            }
        }

        #endregion

    }
}
